/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loltimer;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Platform;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;
import org.apache.log4j.Logger;
import org.jnativehook.GlobalScreen;
import org.jnativehook.NativeHookException;
import org.jnativehook.keyboard.NativeKeyEvent;
import org.jnativehook.keyboard.NativeKeyListener;

/**
 *
 * @author Quentin
 */
public class KeyListener implements NativeKeyListener {

    private Logger log = Logger.getLogger(KeyListener.class);
    private Timeline timelineBlueBuff;
    private Timeline timelineRedBuff;
    private Timeline timelineDragonBuff;
    private Timeline timelineNashorBuff;
    private static final int TIMER_GOLEM_BUF = (300);
    private static final int TIMER_DRAGON_BUF = (360);
    private static final int TIMER_NASHOR_BUF = (420);
    private Integer timeSecondsBlueBuff = new Integer(TIMER_GOLEM_BUF);
    private Integer timeSecondsRedBuff = new Integer(TIMER_GOLEM_BUF);
    private Integer timeSecondsDragonBuff = new Integer(TIMER_DRAGON_BUF);
    private Integer timeSecondsNashorBuff = new Integer(TIMER_NASHOR_BUF);
    private Label labelBlueBuff;
    private Label labelRedBuff;
    private Label labelDagonBuff;
    private Label labelNashorBuff;

    public KeyListener(Label timeblue, Label timered, Label timedragon, Label timenash) {
        try {
            labelBlueBuff = timeblue;
            labelRedBuff = timered;
            labelDagonBuff = timedragon;
            labelNashorBuff = timenash;
            GlobalScreen.registerNativeHook();

        } catch (NativeHookException ex) {
            System.err.println("There was a problem registering the native hook.");
            System.err.println(ex.getMessage());
            System.exit(1);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        System.out.println("reday to listen");
    }

    @Override
    public void nativeKeyPressed(NativeKeyEvent e) {

//        System.out.println("Key Pressed: " + NativeKeyEvent.getKeyText(e.getKeyCode()));

        if (e.getKeyCode() == NativeKeyEvent.VK_ESCAPE) {
            GlobalScreen.unregisterNativeHook();
        }
        switch (NativeKeyEvent.getKeyText(e.getKeyCode())) {
            case "F1":
                log.info("F1 typed -> timer our blue");

                if (timelineBlueBuff != null) {
                    timelineBlueBuff.stop();
                    timeSecondsBlueBuff = TIMER_GOLEM_BUF;
                }
                timelineBlueBuff = new Timeline();
                timelineBlueBuff.setCycleCount(Timeline.INDEFINITE);
                timelineBlueBuff.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler() {
                    @Override
                    public void handle(Event t) {
                        timeSecondsBlueBuff--;
                        // update timerLabel
                        labelBlueBuff.setText(timeSecondsBlueBuff.toString());
                        if (timeSecondsBlueBuff <= 0) {
                            timelineBlueBuff.stop();
                        }
                    }
                }));
                timelineBlueBuff.playFromStart();
                break;

            case "F2":
                log.info("F2 typed -> timer our red");

                if (timelineRedBuff != null) {
                    timelineRedBuff.stop();
                    timeSecondsRedBuff = 20;
                }
                timelineRedBuff = new Timeline();
                timelineRedBuff.setCycleCount(Timeline.INDEFINITE);
                timelineRedBuff.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler() {
                    @Override
                    public void handle(Event t) {
                        timeSecondsRedBuff--;
                        // update timerLabel
                        labelRedBuff.setText(timeSecondsRedBuff.toString());
                        if (timeSecondsRedBuff <= 0) {
                            timelineRedBuff.stop();
                        }
                    }
                }));
                timelineRedBuff.playFromStart();
                break;

            case "F3":
                log.info("F3 typed -> timer dragon");

                if (timelineDragonBuff != null) {
                    timelineDragonBuff.stop();
                    timeSecondsDragonBuff = 20;
                }
                timelineDragonBuff = new Timeline();
                timelineDragonBuff.setCycleCount(Timeline.INDEFINITE);
                timelineDragonBuff.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler() {
                    @Override
                    public void handle(Event t) {
                        timeSecondsDragonBuff--;
                        // update timerLabel
                        labelDagonBuff.setText(timeSecondsDragonBuff.toString());
                        if (timeSecondsDragonBuff <= 0) {
                            timelineDragonBuff.stop();
                        }
                    }
                }));
                timelineDragonBuff.playFromStart();
                break;

            case "F4":
                log.info("F4 typed -> timer nashor");

                if (timelineNashorBuff != null) {
                    timelineNashorBuff.stop();
                    timeSecondsNashorBuff = 20;
                }
                timelineNashorBuff = new Timeline();
                timelineNashorBuff.setCycleCount(Timeline.INDEFINITE);
                timelineNashorBuff.getKeyFrames().add(new KeyFrame(Duration.seconds(1), new EventHandler() {
                    @Override
                    public void handle(Event t) {
                        timeSecondsNashorBuff--;
                        // update timerLabel
                        labelNashorBuff.setText(timeSecondsNashorBuff.toString());
                        if (timeSecondsNashorBuff <= 0) {
                            timelineNashorBuff.stop();
                        }
                    }
                }));
                timelineNashorBuff.playFromStart();
                break;
        }

//        try {
//            synchronized (lock) {
//            nb++;
//            System.out.println("nb element liste " + nb);
//            System.out.println("liste " + listKeyPressed.size());
//            listKeyPressed.add(NativeKeyEvent.getKeyText(e.getKeyCode()));
//            System.out.println(listKeyPressed.get(nb - 1));
//
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }

    }

    @Override
    public void nativeKeyReleased(NativeKeyEvent e) {
//        System.out.println("Key Released: " + NativeKeyEvent.getKeyText(e.getKeyCode()));
//        try {
//            synchronized (lock) {
//            nb--;
//            System.out.println("nb element liste " + nb);
//            System.out.println("liste " + listKeyPressed.size());
//            listKeyPressed.remove(NativeKeyEvent.getKeyText(e.getKeyCode()));
//            System.out.println(listKeyPressed.size());
//
//            }
//        } catch (Exception ex) {
//            ex.printStackTrace();
//        }
    }

    @Override
    public void nativeKeyTyped(NativeKeyEvent e) {
        //System.out.println("Key Typed: " + (e.getKeyChar()));


    }
}
